package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.MerchantDao;
import io.renren.modules.sys.service.MerchantService;


@Service("merchantService")
public class MerchantServiceImpl extends ServiceImpl<MerchantDao, MerchantEntity> implements MerchantService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String name = null;
        if( params.get("name")!=null&&!params.get("name").equals("")){//商户名称
            name =params.get("name").toString();
        }

        String address = null;
        if( params.get("address")!=null&&!params.get("address").equals("")){//店铺地址
            address =params.get("address").toString();
        }
        Long roleId = sysUserRoleDao.queryRoleId(user.getUserId());
        String ids = "";
        String roles = null;
        if(roleId!=null) {
            SysRoleEntity role = sysRoleService.getById(roleId);

            if(role!=null){
                if (role.getRoleName().indexOf("代理商") != -1) {//当前登陆用户为代理商
                    List<SysUserEntity> list = sysUserService.getSales(Integer.parseInt(user.getUserId().toString()));
                    if(list!=null && list.size()>0){
                        for(int i=0;i<list.size();i++){
                            ids += list.get(i).getUserId();
                            if(i!=list.size()-1){
                                ids += ",";
                            }
                        }
                    }
                    roles = "代理商";
                }else if(role.getRoleName().indexOf("业务员") != -1){
                    roles = "业务员";
                }else if(role.getRoleName().indexOf("商户") != -1){
                    roles = "商户";
                }
            }
        }

        int count = baseMapper.queryListcount(name,user.getUserId(),address,roles,ids);
        List<MerchantVOEntity> list = baseMapper.getMerchant(name,user.getUserId(),address,roles,ids,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);

    }

    @Override
    public PageUtils merchantlist(Map<String, Object> params) {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String name = null;
        if( params.get("name")!=null&&!params.get("name").equals("")){//商户名称
            name =params.get("name").toString();
        }
        String address = null;
        if( params.get("address")!=null&&!params.get("address").equals("")){//商户地址
            address =params.get("agent").toString();
        }

        int count = baseMapper.querycount(name,user.getUserId(),address);
        List<MerchantVOEntity> list = baseMapper.getMerchantList(name,user.getUserId(),address,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);

    }

    @Override
    public PageUtils merchantPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String name = null;
        if( params.get("name")!=null&&!params.get("name").equals("")){//商户名称
            name =params.get("name").toString();
        }
        String address = null;
        if( params.get("address")!=null&&!params.get("address").equals("")){//商户地址
            address =params.get("agent").toString();
        }
        Long userid = null;
        if( params.get("userid")!=null&&!params.get("userid").equals("")){//业务员id
            userid =Long.valueOf(params.get("userid").toString());
        }

        int count = baseMapper.querycount(name,userid,address);
        List<MerchantVOEntity> list = baseMapper.getMerchantList(name,userid,address,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);

    }

    @Override
    public MerchantEntity infoByUserid(Integer id){
        MerchantEntity entity = baseMapper.infoByUserid(id);
        return entity;
    }

    @Override
    public int queryLineCodeNum(Integer userid){
        int num = baseMapper.queryLineCodeNum(userid);
        return num;
    }

    @Override
    public MerchantEntity getMerchantByUserid(Integer userid){
        MerchantEntity entity = baseMapper.infoByUserid(userid);
        return entity;
    }
}
